﻿using Google.ProtocolBuffers;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace LightingServer
{
    public class LightingEditor
    {
        public void Edit(DbConnection conn, Lighting lighting)
        {
            StringBuilder builder = new StringBuilder();
            DbCommand command = conn.CreateCommand();
            if (lighting.HasSeries)
            {
                builder.Append(",series=@series");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@series",
                    DbType = DbType.String,
                    Value = lighting.Series
                });
            }

            if (lighting.HasClass)
            {
                builder.Append(",class=@class");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@class",
                    DbType = DbType.UInt32,
                    Value = lighting.Class
                });
            }

            if (lighting.HasPlace)
            {
                builder.Append(",place=@place");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@place",
                    DbType = DbType.UInt32,
                    Value = lighting.Place
                });
            }

            if (lighting.HasVersion)
            {
                builder.Append(",version=@version");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@version",
                    DbType = DbType.String,
                    Value = lighting.Version
                });
            }

            if (lighting.HasLable)
            {
                builder.Append(",lable=@lable");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@lable",
                    DbType = DbType.String,
                    Value = lighting.Lable
                });
            }

            if (lighting.HasOrgin)
            {
                builder.Append(",orgin=@orgin");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@orgin",
                    DbType = DbType.UInt32,
                    Value = lighting.Orgin
                });
            }

            if (command.Parameters.Count > 0)
            {
                builder.Remove(0, 1);
                builder.Insert(0, "UPDATE lighting SET ");
                builder.Append(" WHERE id=@id");
                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@id",
                    DbType = DbType.String,
                    Value = lighting.Id
                });

                command.CommandText = builder.ToString();
                command.ExecuteNonQuery();
            }

            if (lighting.HasPhysical)
            {
                EditPysical(conn.CreateCommand(), lighting.Physical);
            }

            if (lighting.HasOptics)
            {
                EditOptics(conn.CreateCommand(), lighting.Optics);
            }

            if (lighting.HasElectric)
            {
                EditElectric(conn.CreateCommand(), lighting.Electric);
            }

            if (lighting.HasAdapterLighting)
            {
                EditAdapterLighting(conn.CreateCommand(), lighting.AdapterLighting);
            }

            if (lighting.HasProject)
            {
                EditProject(conn.CreateCommand(), lighting.Project);
            }

            if (lighting.HasBusiness)
            {
                EditBusiness(conn.CreateCommand(), lighting.Business);
            }

            if (lighting.HasPicture)
            {
                EditPicture(conn.CreateCommand(), lighting.Picture);
            }
        }

        private void EditPysical(DbCommand command, Physical physical)
        {
            StringBuilder builder = new StringBuilder();

            if (physical.HasMaterial)
            {
                builder.Append(",material=@material");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@material",
                    DbType = DbType.String,
                    Value = physical.Material
                });
            }

            if (physical.HasColor)
            {
                builder.Append(",color=@color");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@color",
                    DbType = DbType.String,
                    Value = physical.Color
                });
            }

            if (physical.HasSize)
            {
                builder.Append(",size=@size");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@size",
                    DbType = DbType.String,
                    Value = physical.Size
                });
            }

            if (physical.HasDefend)
            {
                builder.Append(",defend=@defend");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@defend",
                    DbType = DbType.String,
                    Value = physical.Defend
                });
            }

            if (physical.HasImpact)
            {
                builder.Append(",impact=@impact");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@impact",
                    DbType = DbType.UInt32,
                    Value = physical.Impact
                });
            }

            if (physical.HasWeight)
            {
                builder.Append(",weight=@weight");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@weight",
                    DbType = DbType.String,
                    Value = physical.Weight
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE physical SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = physical.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }

        private void EditOptics(DbCommand command, Optics optics)
        {
            StringBuilder builder = new StringBuilder();

            if (optics.HasExplain)
            {
                builder.Append(",explain=@explain");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@explain",
                    DbType = DbType.String,
                    Value = optics.Explain
                });
            }

            if (optics.HasAngle)
            {
                builder.Append(",angle=@angle");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@angle",
                    DbType = DbType.Double,
                    Value = optics.Angle
                });
            }


            if (optics.HasEfficiency)
            {
                builder.Append(",efficiency=@efficiency");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@efficiency",
                    DbType = DbType.Double,
                    Value = optics.Efficiency
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE optics SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = optics.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }

        private void EditElectric(DbCommand command, Electric electric)
        {
            StringBuilder builder = new StringBuilder();

            if (electric.HasShock)
            {
                builder.Append(",shock=@shock");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@shock",
                    DbType = DbType.String,
                    Value = electric.Shock
                });
            }

            if (electric.HasVoltage)
            {
                builder.Append(",voltage=@voltage");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@voltage",
                    DbType = DbType.UInt32,
                    Value = electric.Voltage
                });
            }

            if (electric.HasType)
            {
                builder.Append(",type=@type");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@type",
                    DbType = DbType.Int32,
                    Value = electric.Type
                });
            }

            if (electric.HasElectricPower)
            {
                builder.Append(",electricPower=@electricPower");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@electricPower",
                    DbType = DbType.UInt32,
                    Value = electric.ElectricPower
                });
            }

            if (electric.HasLightingPower)
            {
                builder.Append(",lightingPower=@lightingPower");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@lightingPower",
                    DbType = DbType.UInt32,
                    Value = electric.LightingPower
                });
            }

            if (electric.HasAllPower)
            {
                builder.Append(",allPower=@allPower");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@allPower",
                    DbType = DbType.UInt32,
                    Value = electric.AllPower
                });
            }

            if (electric.HasControlType)
            {
                builder.Append(",controlType=@controlType");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@controlType",
                    DbType = DbType.UInt32,
                    Value = electric.ControlType
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE electric SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = electric.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }

        private void EditAdapterLighting(DbCommand command, AdapterLighting adapterLighting)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("INSERT INTO electric(id");

            if (adapterLighting.HasClass)
            {
                builder.Append(",class=@class");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@class",
                    DbType = DbType.UInt32,
                    Value = adapterLighting.Class
                });
            }

            if (adapterLighting.HasVersion)
            {
                builder.Append(",version=@version");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@version",
                    DbType = DbType.String,
                    Value = adapterLighting.Version
                });
            }

            if (adapterLighting.HasColor)
            {
                builder.Append(",color=@color");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@color",
                    DbType = DbType.String,
                    Value = adapterLighting.Color
                });
            }

            if (adapterLighting.HasCri)
            {
                builder.Append(",cri=@cri");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@cri",
                    DbType = DbType.UInt32,
                    Value = adapterLighting.Cri
                });
            }

            if (adapterLighting.HasOutput)
            {
                builder.Append(",output=@output");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@output",
                    DbType = DbType.UInt32,
                    Value = adapterLighting.Output
                });
            }

            if (adapterLighting.HasPins)
            {
                builder.Append(",pins=@pins");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@pins",
                    DbType = DbType.String,
                    Value = adapterLighting.Pins
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE adapterLighting SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = adapterLighting.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }

        private void EditProject(DbCommand command, Project project)
        {
            StringBuilder builder = new StringBuilder();

            if (project.HasParts)
            {
                builder.Append(",parts=@parts");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@parts",
                    DbType = DbType.String,
                    Value = project.Parts
                });
            }

            if (project.HasSetup)
            {
                builder.Append(",setup=@setup");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@setup",
                    DbType = DbType.UInt32,
                    Value = project.Setup
                });
            }

            if (project.HasSize)
            {
                builder.Append(",size=@size");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@size",
                    DbType = DbType.String,
                    Value = project.Size
                });
            }

            if (project.HasReport)
            {
                builder.Append(",report=@report");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@report",
                    DbType = DbType.String,
                    Value = project.Report
                });
            }

            if (project.HasBaikup)
            {
                builder.Append(",baikup=@baikup");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@baikup",
                    DbType = DbType.String,
                    Value = project.Baikup
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE project SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = project.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }

        private void EditBusiness(DbCommand command, Business business)
        {
            StringBuilder builder = new StringBuilder();

            if (business.HasIn)
            {
                builder.Append(",in=@in");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@in",
                    DbType = DbType.Double,
                    Value = business.In
                });
            }

            if (business.HasMarket)
            {
                builder.Append(",market=@market");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@market",
                    DbType = DbType.Double,
                    Value = business.Market
                });
            }

            if (business.HasOut)
            {
                builder.Append(",out=@out");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@out",
                    DbType = DbType.Double,
                    Value = business.Out
                });
            }

            if (business.HasDelievery)
            {
                builder.Append(",delievery=@delievery");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@delievery",
                    DbType = DbType.String,
                    Value = business.Delievery
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE business SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = business.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }

        private void EditPicture(DbCommand command, Picture picture)
        {
            StringBuilder builder = new StringBuilder();
            if (picture.HasFacade)
            {
                builder.Append(",facade=@facade");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@facade",
                    DbType = DbType.Binary,
                    Value = picture.Facade
                });
            }

            if (picture.HasSize)
            {
                builder.Append(",size=@size");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@size",
                    DbType = DbType.Binary,
                    Value = picture.Size
                });
            }

            if (picture.HasGrading1)
            {
                builder.Append(",grading1=@grading1");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@grading1",
                    DbType = DbType.Binary,
                    Value = picture.Grading1
                });
            }

            if (picture.HasLive)
            {
                builder.Append(",live=@live");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@live",
                    DbType = DbType.Binary,
                    Value = picture.Live
                });
            }

            builder.Remove(0, 1);
            builder.Insert(0, "UPDATE picture SET ");
            builder.Append(" WHERE id=@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = picture.Id
            });

            command.CommandText = builder.ToString();
            command.ExecuteNonQuery();
        }
    }
}
